package com.server.server;

import org.apache.mina.core.session.IoSession;

import com.server.app.PlayerManager;
import com.server.object.Player;
import com.server.protocols.SProtocols;
import com.server.request.LeaveRoomReq;
import com.server.response.LeaveRoomResp;
import com.server.status.SocketState;
import com.webserver.server.net.BaseMessage;
import com.webserver.server.net.BaseServer;

public class LeaveRoomServer implements BaseServer {
	public int GetProtocol() {
		return SProtocols.LEAVE_ROOM;
	}

	public BaseMessage GetRequest() {
		return new LeaveRoomReq();
	}

	public BaseMessage handle(IoSession is,BaseMessage request) {
		Player player = PlayerManager.GetInstance().GetPlayer(request.getUserInfo().uid);
		LeaveRoomResp resp;
		if(player != null) {
			resp = new LeaveRoomResp(SocketState.SUCCESSED.ordinal());
			if(player.room != null) {
				player.room.RemovePlayer(player.uid);
				player.room = null;
			}else {
				System.out.println("room 是null的");
			}
		}else {
			System.out.println("player 是null的");
			resp = new LeaveRoomResp(SocketState.RELOGIN.ordinal());
		}
		return resp;
	}
}
